import Question from "./Question";
import OneOfFour from "./OneOfFour";

export default class QuestionList {
  name:String;
  list:Array<Question>;
  randomList:Array<number>;
  oneOfFourList:Array<OneOfFour>
  type:string
  constructor(type:string) {
    this.list=new Array<Question>()
    this.randomList=new Array<number>()
    this.oneOfFourList=new Array<OneOfFour>()
    this.type=type
  }
  async initData(pageId:string){
  }
  async getProficiency(pageId){
    let proficiency=0
    for (let index in this.list){
      let question=this.list[index]
      if(question.proficiency){
        proficiency+=question.proficiency
      }
    }
    //熟练度应该除以总体数
    // console.log(proficiency)
    // console.log(this.list)
    return (proficiency/this.list.length*100).toFixed(1)
  }
  //获取n道题目
  randomN(n:number){
    this.randomList.length=0
    this.oneOfFourList.length=0;
    for (var i=0;i<n;i++){
      this.oneOfFourList.push(this.randomOneOfFour())
    }
    // console.log(this.oneOfFourList)
  }
  createErrorList(oneOfFour){
    //随机一个方向 0:up 1:right 2:down 3:left
    let directionIndex=Math.floor(Math.random()*4);
    oneOfFour.rightDirection=directionIndex;
    let answer=parseInt(oneOfFour.question.answer)
    //围绕正确答案，随机4个错误答案，去重
    let errorAnswerList=this.randomErrorAnswerList(answer)
    oneOfFour.answerUp=''+errorAnswerList[0];
    oneOfFour.answerRight=''+errorAnswerList[1];
    oneOfFour.answerDown=''+errorAnswerList[2];
    oneOfFour.answerLeft=''+errorAnswerList[3];
    if (directionIndex==0){
      oneOfFour.answerUp=''+answer
    }else if(directionIndex==1){
      oneOfFour.answerRight=''+answer
    }else if(directionIndex==2){
      oneOfFour.answerDown=''+answer
    }else{
      oneOfFour.answerLeft=''+answer
    }
  }
  randomOneOfFour(){
    var index=Math.floor(Math.random()*this.list.length);
    while(this.randomList.indexOf(index)!=-1){
      index=Math.floor(Math.random()*this.list.length);
    }
    this.randomList.push(index)
    let question=this.list[index];
    var oneOfFour=new OneOfFour();
    oneOfFour.question=question;
    this.createErrorList(oneOfFour);
    return oneOfFour
  }
  randomErrorAnswerList(answer){
    let errorAnswerList=[]
    for (let i=0;i<16;i++){
      let between=Math.floor(Math.random()*40)-20;
      let errorAnswer = Math.abs(answer+between);
      if (errorAnswerList.indexOf(errorAnswer)==-1&&
        errorAnswer!=0&&errorAnswer!=answer){
        errorAnswerList.push(errorAnswer)
      }
    }
    return errorAnswerList

  }
}
